//
//  transferVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class transferVC: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    
    var modelList:[mySelfTransferInfo]?{
        didSet{
            if self.modelList?.count != 0{
                self.initRightItem()
                self.tableView.reloadData()
            }
            
        }
    }
    var msg: regiestInfo?{
        didSet{
            self.tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        self.getTransfer()

    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    //MARK: initRightItem
    func initRightItem(){
        let shareBtn = UIButton(frame: CGRectMake(0, 0, 25, 25))
        shareBtn.setBackgroundImage(UIImage(named: "right_item")!, forState: UIControlState.Normal)
        shareBtn.addTarget(self, action: #selector(self.shareAction), forControlEvents:  UIControlEvents.TouchUpInside)
        let share = UIBarButtonItem(customView: shareBtn)
        self.navigationItem.rightBarButtonItem = share
    }
    
    func shareAction(){
        self.tableView.editing = !self.tableView.editing
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if let count = self.modelList?.count {
            return count
        }
        return 0
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 100.0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("transferCell", forIndexPath: indexPath) as! transferCell
        let model = modelList![indexPath.row]
        cell.iv.sd_setImageWithURL(NSURL(string: model.imgsrc), placeholderImage: UIImage(named: "placeholder"))
        cell.name.text = model.name
        cell.address.text = String(format: "%@|%@",model.area, model.address)
        cell.newold.text = model.newold
        cell.price.text = model.price
        cell.type.text = model.type
        cell.ptime.text = publicMethod.computationTimeDifference(model.ptime)
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        if self.modelList![indexPath.row].type == "整店"{
            let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
            let vc = sb.instantiateViewControllerWithIdentifier("TransferShopDetailTableViewController") as! TransferShopDetailTableViewController
            
            vc.id = Int(self.modelList![indexPath.row].id)!
            vc.type = self.modelList![indexPath.row].type
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
            let vc = sb.instantiateViewControllerWithIdentifier("TransferGoodsDetailTalbeViewController") as! TransferGoodsDetailTalbeViewController
            
            vc.id = Int(self.modelList![indexPath.row].id)!
            vc.type = self.modelList![indexPath.row].type
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    //删除某行cell
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
                   forRowAtIndexPath indexPath: NSIndexPath){
        if(editingStyle == UITableViewCellEditingStyle.Delete) {
            
            let model = modelList![indexPath.row]
            self.getDelegateInfo(model.id)
           
        }
        
    }
    //是否打开可以编辑
    func tableView(tableView: UITableView,
                   canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        
        return true
        
    }
    //是否可以滑动删除
    func tableView(tableView: UITableView,
                   canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        return true
        
    }
    //编辑样式
    func tableView(tableView: UITableView,
                   editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle{
        
        return .Delete
        
    }
    //MARK:networking
    //获取信息
    func getTransfer(){
        DataTool.mySelfTransfer({ (newModel) in guard let newDataes = newModel else {
            return
            }
            print(newDataes)
            self.modelList = newDataes
            self.tableView.reloadData()
        })

    }
    //删除信息
    func getDelegateInfo(id:String){
        DataTool.delegateMyTransfer(id, completionHandler: { (newModel) in
            guard let newDataes = newModel else {
                return
            }
    
            print(newDataes)
            self.msg = newDataes
            self.tableView.reloadData()
            self.getTransfer()
            SVProgressHUD.showSuccessWithStatus(self.msg!.msg)
           
            
        })

        
    }
}
